//<script type='text/javascript'>

	////////////////////////////////////////// EXTENTION: BAR: translate
var Translate = function ( vars ) {
	Object.extend(obj = new Element("div", {id:"translate", class:"ext translate"}), dashboard.ext.translate);
	
	//// BUILD TRANSLATE GRID
	obj.insert( obj.grid = new Unit( "Translate.", "ul", init.langs.length ));
	
	//obj.insert( grid = new Element( "ul", { class: "unit columns" + init.langs.length }));
	//grid.insert( li = new Element( "li" ).update( new Element( "h1", { class: "descr" }).update( "Translate." )));
	//init.langs.each( function( lng ){ this.insert( new Element( "span", { class: "lang" }).update( lng ))}, li );
	
	init.langs.each( function( lng ){
		this.insert({ before: new Element( "span", { class: "lang" }).update( lng )})
	}, obj.grid.down(".content") );
	
	return obj.request();
}


dashboard.ext.translate	= {
	selected	: 0,
	addbutton	: function() {
		obj = new Element("div", { id:"dashboard.ext.translatebutton", class:"ext_button translate", name:"ext:translate" }).observe('click', dashboard.showhide );
		obj.ext = Translate;
		return obj;
	},

	request		: function() {

		new Ajax.Request( 'control.call.php', { parameters:{ set:Object.toJSON({type:'text', action:"dictionary", transfer_type: "js" })}, evalJSON: 'force', onComplete: this.seed.bind( this )});
		return this;
	},
	
	seed	: function( dict ) {
		
		$H( dict.responseJSON ).each( function( o ){
			this.grid.content.insert( this.line( o.key, o.value ));
		}, this );
		
		if( dict.responseJSON )
			this.grid.content.insert( new Element( "li" ).update( new Element( "span", { class:"plus" }).observe( "click", this.add.bind( this ) )));
			
		this.grid.enableScroll( 320 );
		
	},
	
	transl	: function( e ) {
		
		//console.log( this );
		// get keys
		key = e.currentTarget.up().select( 'input.descr' );
		key = key[0];
		
		// get strings
		strings = key.up().select( 'input' );
		strings.each( function( i ){
				
			if( i != key ) {
				
				new Ajax.Request( 'control.call.php', { parameters:{set:Object.toJSON({type:'text', action:"auto_translate", transfer_type: "js", key: key.value, lang: i.vars.lang })}, evalJSON: 'force', onComplete: this.process.bind( this, i )});
			}
			
		}, this );
	
	},
	
	process	: function( response, what ) {
		
		response.value = what.responseText;
	
	},
	
	line	: function( key, vars ) {
		
		li = new Element("li").update( descr = new Element( "input", { type:"text", class: "descr", value: key }).observe( "change", this.update ));
		descr.vars = { key: key, is_key: true };
		
		init.langs.each( function( lang ){
			li.insert( el = new Element( "input", { type: "text", class: lang, value: ( vars[ lang ])? vars[ lang ] : "" }).observe( "change", this.update ));
			el.vars = { key: key, lang: lang };
		}, this );
		
		li.insert( el = new Element( "span", { class: "plus robot" }).observe( "click", this.transl.bind( this )));
		
		return li;
	},
	
	add		: function( e ) {
		console.log(  )
		e.currentTarget.insert( {before: this.line( "...", {} )});
	},
	
	update	: function( e ) {

		this.vars.value = this.value;
		xhttp.send( 'control.call.php', { transfer_type:'push', type:"text", action:"dictionary_update", vars:this.vars });
		
		if( this.vars.is_key )
			this.up().select( "input" ).each( function( el ){ el.vars.key = this.value; }, this );
	}
}
